package com.fc.test.api;

import com.fc.test.model.auto.TAbbMember;
import com.fc.test.model.auto.TAbbRenzheng;
import com.fc.test.service.TAbbMemberService;
import com.fc.test.service.TAbbRenzhengService;
import com.fc.test.util.DateUtil;
import com.fc.test.util.JsonUtil;
import com.fc.test.util.SnowflakeIdWorker;
import com.fc.test.util.wx.HttpUtilWX;
import io.swagger.annotations.*;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.net.ssl.SNIHostName;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/apiMemberController")
@Api(value = "用户")
public class ApiMemberController {

    @Autowired
    private TAbbMemberService memberService;

    @Autowired
    private TAbbRenzhengService renzhengService;


    @ResponseBody
    @PostMapping(value="xcxLogin",produces = {"application/json; charset=utf-8"})
    @ApiOperation(value = "小程序登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name="code",value = "code",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "nicename", value = "微信昵称", dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "wximages", value = "微信头像", dataType = "String",paramType = "query"),
            @ApiImplicitParam(name="yqrId",value = "上级id",dataType = "String",paramType = "query"),
    })
    public String xcxLogin(String code,String nicename,String wximages,String yqrId){
        Map<String,Object> resMap = new HashMap<>();
        // 第二步：通过code换取网页授权access_token
        String o_auth_openid_url = "https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code="
                + code + "&grant_type=authorization_code";
        String requestUrl = o_auth_openid_url.replace("APPID","wxd2eb33a60062405f").replace("SECRET","3cce6fec502ad76416901fe57b774330").replace("CODE",
                code);
        String resultInfo = HttpUtilWX.doGet(requestUrl);
        System.out.println("+++++++++++++++++" + resultInfo);
        JSONObject jsonObject = JSONObject.fromObject(resultInfo);
        String openId = jsonObject.getString("openid");

        resMap = memberService.registerMember(openId,nicename,wximages,yqrId);
        return JsonUtil.jsonMapStr(resMap);
    }

    @ResponseBody
    @PostMapping(value="getMemberInfo",produces = {"application/json; charset=utf-8"})
    @ApiOperation(value = "获取用户信息")
    public String getMemberInfo(
            @ApiParam(value = "用户id") @RequestParam(value = "memberid") String memberid
    ){
        Map<String,Object> resMap = new HashMap<>();
        TAbbMember member = memberService.selectByPrimaryKey(memberid);
        if(member!=null){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            if(null!=member.getVipendtime()&&!"".equals(member.getVipendtime())){
                String vipEndDate = sdf.format(member.getVipendtime());
                resMap.put("vipEndDate",vipEndDate);
            }else{
                resMap.put("vipEndDate","");
            }
            resMap.put("success",true);
            resMap.put("msg","获取用户信息成功");
            resMap.put("member",member);

        }else{
            resMap.put("success",false);
            resMap.put("msg","获取用户信息失败");
        }
        return JsonUtil.jsonMapStr(resMap);
    }

    @ResponseBody
    @PostMapping(value="updateMemberInfo",produces = {"application/json; charset=utf-8"})
    @ApiOperation(value = "修改用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name="id",value = "用户id",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "phone", value = "电话", dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "qq", value = "qq", dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "address", value = "地址", dataType = "String",paramType = "query")
    })
    public String updateMemberInfo(String id,String phone,String qq,String address){
        Map<String,Object> resMap = new HashMap<>();
        int flag = memberService.updateMemberInfo(id,phone,qq,address);
        if(flag>0){
            resMap.put("success",true);
            resMap.put("msg","保存成功");
        }else{
            resMap.put("success",false);
            resMap.put("msg","保存失败");
        }
        return JsonUtil.jsonMapStr(resMap);
    }


    @ResponseBody
    @PostMapping(value="getFundRecords",produces = {"application/json; charset=utf-8"})
    @ApiOperation(value = "获取用户资金记录")
    public String getFundRecords(
            @ApiParam(value = "用户id") @RequestParam(value = "memberid") String memberid,
            @ApiParam(value = "1-收益记录；2-提现记录；3-消费记录") @RequestParam(value = "type") int type,
            @ApiParam(value = "页码") @RequestParam(value = "page") int page
    ){
        Map<String,Object> resMap = new HashMap<>();
        List<?> list = memberService.getFundRecords(memberid,type,page);
        TAbbMember member = memberService.selectByPrimaryKey(memberid);
        resMap.put("account",member.getAccount());
        resMap.put("success",true);
        resMap.put("list",list);
        if(list.size()>0){
            if(type==1){
                resMap.put("msg","获取收益记录成功");
            }else if(type==2){
                resMap.put("msg","获取提现记录成功");
            }else if(type==3){
                resMap.put("msg","获取消费记录成功");
            }
        }else{
            resMap.put("msg","到底了");
        }
        return JsonUtil.jsonMapStr(resMap);
    }


    @ResponseBody
    @PostMapping(value="applyRenZheng",produces = {"application/json; charset=utf-8"})
    @ApiOperation(value = "申请商户认证")
    public String applyRenZheng(
            @ApiParam(value = "用户id") @RequestParam(value = "userid") String userid,
            @ApiParam(value = "商铺名称") @RequestParam(value = "shopname") String shopname,
            @ApiParam(value = "电话") @RequestParam(value = "phone") String phone,
            @ApiParam(value = "联系人姓名") @RequestParam(value = "lianxiname") String lianxiname,
            @ApiParam(value = "身份证号") @RequestParam(value = "idcard") String idcard,
            @ApiParam(value = "商家地址") @RequestParam(value = "address") String address,
            @ApiParam(value = "身份证正面") @RequestParam(value = "zimage") String zimage,
            @ApiParam(value = "身份证背面") @RequestParam(value = "fimage") String fimage,
            @ApiParam(value = "经营证件照") @RequestParam(value = "jimages") String jimages,
            @ApiParam(value = "商家类型") @RequestParam(value = "kinds") String kinds,
            @ApiParam(value = "商家图片") @RequestParam(value = "shopimg") String shopimg
    ){
        Map<String,Object> resMap = new HashMap<>();
        TAbbRenzheng renzheng = renzhengService.getRenzhengStatus(userid);
        String act = "edit";
        int flag = 0;
        if(null==renzheng){
            renzheng = new TAbbRenzheng();
            act = "add";
        }
        TAbbMember member = new TAbbMember();
        renzheng.setId(SnowflakeIdWorker.getUUID());
        renzheng.setCreattime(DateUtil.getCurrentDateTimeStr());
        renzheng.setUserid(userid);
        renzheng.setUsername(member.getNicename());
        renzheng.setShopname(shopname);
        renzheng.setPhone(phone);
        renzheng.setLianxiname(lianxiname);
        renzheng.setIdcard(idcard);
        renzheng.setAddress(address);
        renzheng.setZimage(zimage);
        renzheng.setFimage(fimage);
        renzheng.setJimages(jimages);
        renzheng.setYuliu1("0");
        renzheng.setYuliu2(kinds);
        renzheng.setYuliu3(shopimg);
        if("add".equals(act)){
            flag = memberService.applyRenZheng(renzheng);
        }else{
            renzheng.setStatus(0);
            renzheng.setFailreason("");
            flag = renzhengService.updateByPrimaryKeySelective(renzheng);
        }
        if(flag>0){
            resMap.put("success",true);
            resMap.put("msg","提交成功，请耐心等待审核");
        }else{
            resMap.put("success",false);
            resMap.put("msg","提交失败");
        }
        return JsonUtil.jsonMapStr(resMap);
    }


    @ResponseBody
    @PostMapping(value="getRenzhengStatus",produces = {"application/json; charset=utf-8"})
    @ApiOperation(value = "获取认证状态")
    public String getRenzhengStatus(
            @ApiParam(value = "用户id") @RequestParam(value = "userid") String userid
    ){
        Map<String,Object> resMap = new HashMap<>();
        TAbbRenzheng renzheng = renzhengService.getRenzhengStatus(userid);
        if(renzheng!=null){
            int status = renzheng.getStatus();
            if(0==status){
                resMap.put("msg","审核中");
                resMap.put("flag","1");
            }else if(1==status){
                resMap.put("msg","审核通过");
                resMap.put("flag","2");
            }else if(2==status){
                resMap.put("msg","审核不通过");
                resMap.put("flag","3");
                resMap.put("failreason",renzheng.getFailreason());
            }
        }else{
            resMap.put("msg","尚未认证");
            resMap.put("flag","0");
        }
        resMap.put("success",true);
        return JsonUtil.jsonMapStr(resMap);
    }


    @ResponseBody
    @PostMapping(value="getRenzhengInfo",produces = {"application/json; charset=utf-8"})
    @ApiOperation(value = "获取认证信息")
    public String getRenzhengInfo(
            @ApiParam(value = "用户id") @RequestParam(value = "userid") String userid
    ){
        Map<String,Object> resMap = new HashMap<>();
        TAbbRenzheng renzheng = renzhengService.getRenzhengStatus(userid);
        if(renzheng!=null){
            resMap.put("msg","获取认证信息成功");
            resMap.put("flag","1");
            resMap.put("renzheng",renzheng);
        }else{
            resMap.put("msg","尚未认证");
            resMap.put("flag","0");
        }
        resMap.put("success",true);
        return JsonUtil.jsonMapStr(resMap);
    }

    @ResponseBody
    @PostMapping(value="getMyTeam",produces = {"application/json; charset=utf-8"})
    @ApiOperation(value = "我的团队")
    public String getMyTeam(
            @ApiParam(value = "用户id") @RequestParam(value = "userid") String userid,
            @ApiParam(value = "页码") @RequestParam(value = "page") int page
    ){
        Map<String,Object> resMap = new HashMap<>();
        List<TAbbMember> list = memberService.getMyTeam(userid,page);
        resMap.put("success",true);
        resMap.put("msg","获取团队成员成功");
        resMap.put("list",list);
        return JsonUtil.jsonMapStr(resMap);
    }
}
